/*     */ package com.zimbra.cs.mailclient.imap;
/*     */ 
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.mailclient.ParseException;
/*     */ import com.zimbra.cs.mailclient.util.Ascii;
/*     */ import java.io.IOException;
/*     */ import java.nio.ByteBuffer;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class MailboxName
/*     */ {
/*     */   private String name;
/*  30 */   public static final MailboxName INBOX = new MailboxName("INBOX");
/*     */   
/*  32 */   private static final char[] ENCODE_PEM = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', ',' };
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*  43 */   private static final byte[] DECODE_PEM = new byte['Ā'];
/*     */   
/*  45 */   static { for (int i = 0; i < 256; i++) {
/*  46 */       DECODE_PEM[i] = -1;
/*     */     }
/*  48 */     for (int i = 0; i < ENCODE_PEM.length; i++) {
/*  49 */       DECODE_PEM[ENCODE_PEM[i]] = ((byte)i);
/*     */     }
/*     */   }
/*     */   
/*     */   public static MailboxName decode(ImapData encoded) throws IOException {
/*  54 */     return decode(ByteBuffer.wrap(encoded.getBytes()));
/*     */   }
/*     */   
/*     */   public static MailboxName decode(String encoded) throws ParseException {
/*  58 */     if (StringUtil.isAsciiString(encoded)) {
/*  59 */       return decode(ByteBuffer.wrap(Ascii.getBytes(encoded)));
/*     */     }
/*  61 */     ZimbraLog.imap_client.debug("mailbox name %s not ASCII, assuming server sent it without encoding", new Object[] { encoded });
/*  62 */     return new MailboxName(StringUtil.stripControlCharacters(encoded));
/*     */   }
/*     */   
/*     */   public static MailboxName decode(ByteBuffer bb) throws ParseException
/*     */   {
/*  67 */     return new MailboxName(decodeBytes(bb));
/*     */   }
/*     */   
/*     */   public MailboxName(String name) {
/*  71 */     this.name = name;
/*     */   }
/*     */   
/*     */   private static String decodeBytes(ByteBuffer bb) throws ParseException {
/*  75 */     StringBuffer sb = new StringBuffer(bb.remaining());
/*  76 */     while (bb.hasRemaining()) {
/*  77 */       int c = bb.get();
/*  78 */       if (c < 0) {
/*  79 */         throw new ParseException("Not a 7-bit character: " + c);
/*     */       }
/*  81 */       if (c == 38) {
/*  82 */         decodePEM(bb, sb);
/*     */       } else {
/*  84 */         sb.append((char)c);
/*     */       }
/*     */     }
/*  87 */     return sb.toString();
/*     */   }
/*     */   
/*     */   private static void decodePEM(ByteBuffer bb, StringBuffer sb) throws ParseException
/*     */   {
/*     */     try {
/*  93 */       int c = bb.get();
/*  94 */       if (c == 45) {
/*  95 */         sb.append('&');
/*  96 */         return;
/*     */       }
/*  98 */       int bits = 0;
/*  99 */       int count = 0;
/*     */       do {
/* 101 */         byte b = DECODE_PEM[(c & 0xFF)];
/* 102 */         if (b == -1) {
/* 103 */           throw new ParseException("Invalid Base64 character: " + c);
/*     */         }
/* 105 */         bits = bits << 6 | b;
/* 106 */         count += 6;
/* 107 */         if (count > 15) {
/* 108 */           count -= 16;
/* 109 */           sb.append((char)(bits >> count));
/* 110 */           bits &= (1 << count) - 1;
/*     */         }
/* 112 */       } while ((c = bb.get()) != 45);
/*     */       
/* 114 */       if ((count > 0) && (bits != 0)) {
/* 115 */         throw new ParseException("Invalid Base64 encoding");
/*     */       }
/*     */     } catch (IndexOutOfBoundsException e) {
/* 118 */       throw new ParseException("Unterminated Base64 encoding");
/*     */     }
/*     */   }
/*     */   
/*     */   public String encode() {
/* 123 */     StringBuilder sb = new StringBuilder(this.name.length());
/* 124 */     int index = 0;
/* 125 */     while (index < this.name.length()) {
/* 126 */       char c = this.name.charAt(index);
/* 127 */       if ((c >= ' ') && (c <= '~')) {
/* 128 */         if (c == '&') {
/* 129 */           sb.append('&').append('-');
/*     */         } else {
/* 131 */           sb.append(c);
/*     */         }
/* 133 */         index++;
/*     */       } else {
/* 135 */         index = encodePEM(sb, index);
/*     */       }
/*     */     }
/* 138 */     return sb.toString();
/*     */   }
/*     */   
/*     */   private int encodePEM(StringBuilder sb, int index) {
/* 142 */     sb.append('&');
/* 143 */     int bits = 0;
/* 144 */     int count = 0;
/* 145 */     while (index < this.name.length()) {
/* 146 */       char c = this.name.charAt(index);
/* 147 */       if ((c >= ' ') && (c <= '~')) break;
/* 148 */       index++;
/* 149 */       bits = bits << 16 | c;
/* 150 */       count += 2;
/* 151 */       if (count == 4) {
/* 152 */         encodePEM(sb, bits >>> 8, 3);
/* 153 */         bits &= 0xFF;
/* 154 */         count = 1;
/* 155 */       } else if (count == 3) {
/* 156 */         encodePEM(sb, bits, count);
/* 157 */         bits = 0;
/* 158 */         count = 0;
/*     */       }
/*     */     }
/* 161 */     if (count > 0) {
/* 162 */       encodePEM(sb, bits, count);
/*     */     }
/* 164 */     sb.append('-');
/* 165 */     return index;
/*     */   }
/*     */   
/*     */   private static void encodePEM(StringBuilder sb, int bits, int count) {
/* 169 */     switch (count) {
/*     */     case 3: 
/* 171 */       sb.append(encodePEM(bits, 18));
/* 172 */       sb.append(encodePEM(bits, 12));
/* 173 */       sb.append(encodePEM(bits, 6));
/* 174 */       sb.append(encodePEM(bits, 0));
/* 175 */       break;
/*     */     case 2: 
/* 177 */       bits <<= 2;
/* 178 */       sb.append(encodePEM(bits, 12));
/* 179 */       sb.append(encodePEM(bits, 6));
/* 180 */       sb.append(encodePEM(bits, 0));
/* 181 */       break;
/*     */     case 1: 
/* 183 */       bits <<= 4;
/* 184 */       sb.append(encodePEM(bits, 6));
/* 185 */       sb.append(encodePEM(bits, 0));
/*     */     }
/*     */   }
/*     */   
/*     */   private static char encodePEM(int bits, int shift) {
/* 190 */     return ENCODE_PEM[(bits >> shift & 0x3F)];
/*     */   }
/*     */   
/*     */   public int hashCode()
/*     */   {
/* 195 */     return this.name.hashCode();
/*     */   }
/*     */   
/*     */   public boolean equals(Object obj)
/*     */   {
/* 200 */     return ((obj instanceof MailboxName)) && (this.name.equals(((MailboxName)obj).name));
/*     */   }
/*     */   
/*     */ 
/*     */   public String toString()
/*     */   {
/* 206 */     return this.name;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailclient/imap/MailboxName.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */